//
//  WalletViewController.m
//  TenThousand
//
//  Created by 我的 on 16/6/27.
//  Copyright © 2016年 baixue. All rights reserved.
//

#import "WalletViewController.h"
#import "PNLineChartView.h"
#import "PNPlot.h"
#import "WalletHeaderTableViewCell.h"

#import "IntroduceViewController.h"//萬钱宝介绍
#import "AnswerViewController.h"//问题解答
#import "ProjectIntroductionViewController.h"//资金明细
#import "WalletTableViewCell.h"
#import "TopupViewController.h"
#import "MyPayViewController.h"
@interface WalletViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    UIWindow *_window;
    UIButton *_signView;
    UITextField *_amount;
    
}

/**
 *  存储返回的数据
 */
@property (nonatomic, strong) NSMutableDictionary *dicM;

@property (nonatomic, strong) PNLineChartView *lineChatView;


@end

@implementation WalletViewController

- (NSMutableDictionary *)dicM {
    if (!_dicM) {
        _dicM = [NSMutableDictionary dictionary];
    }
    return _dicM;
}

- (NSMutableDictionary *)dueOnDemandDicM {
    if (!_dueOnDemandDicM) {
        _dueOnDemandDicM = [NSMutableDictionary dictionary];
    }
    return _dueOnDemandDicM;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"萬钱宝";
    [self.view addSubview:self.walletTableView];
    [self createLeft:[UIImage imageNamed:@"ZHSZicon_03"] leftClick:^{
        [self.navigationController popViewControllerAnimated:YES];
    } right:nil rightClick:nil];
//    [self getData];
//    [self getAnnualData];
    
   
    
}

/**
 *  获取收益接口
 */
- (void)getData {
    /*
     Id     Id      int     是
     用户Id  UserId  int     是
     Key    Key     是
     */
    
//    NSString *userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
//    NSDictionary *dict = @{
//                           @"Id":self.dueOnDemandDicM[@"Id"],
//                           @"UserId":userId
//                           };
//    [NetWorkManager requestWithType:HttpRequestTypeGet withUrlString:@"Tend/GetTendMoney" withMethodName:@"GetTendMoney" withParaments:dict withSuccessBlock:^(NSDictionary *object) {
//        NSLog(@"%@", object);
//        [self.dicM addEntriesFromDictionary:[object[@"data"] firstObject]];
//        [self.walletTableView reloadData];
//    } withFailureBlock:^(NSError *error) {
//        
//    } progress:^(float progress) {
//        
//    }];
}

/**
 *  获取折线图收益走势接口
 */
- (void)getAnnualData {
    /*
     标 Id     TendId     int     是
     用户 Id     UserId     int     是
     Key     Key     是
     */
    
//    NSString *userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
//    NSDictionary *dict = @{
//                           @"Id":self.dueOnDemandDicM[@"Id"],
//                           @"UserId":userId
//                           };
//    [NetWorkManager requestWithType:HttpRequestTypeGet withUrlString:@"Tend/GetAnnual" withMethodName:@"GetAnnual" withParaments:dict withSuccessBlock:^(NSDictionary *object) {
//        NSLog(@"%@", object);
//    } withFailureBlock:^(NSError *error) {
//        
//    } progress:^(float progress) {
//        
//    }];
    
}

//折线图
- (PNLineChartView *)lineChatView
{
    
    _lineChatView = [[PNLineChartView alloc] initWithFrame:CGRectMake(10, 10, Screen_Width-25, 250)];
    _lineChatView.backgroundColor = [UIColor whiteColor];
    _lineChatView.max = 5;
    _lineChatView.min = 0;
    _lineChatView.numberOfVerticalElements = 5;
    _lineChatView.interval = 1;
    _lineChatView.horizontalLineInterval = 40;
    _lineChatView.pointerInterval = 45;
    _lineChatView.yAxisValues = @[@"0.00",@"1.00",@"2.00",@"3.00",@"4.00",@"5.00",@"6.00"];
    _lineChatView.xAxisValues  = @[@"05-14", @"05-15", @"05-16",@"05-17", @"05-19", @"05-20"];
    _lineChatView.xAxisFontColor = [UIColor lightGrayColor];
    _lineChatView.axisLeftLineWidth = 39;
    _lineChatView.axisBottomLinetHeight = 39;
    
    NSArray* plottingDataValues1 =@[@5, @5, @5, @5,@5,@5];
    PNPlot *plot2 = [[PNPlot alloc] init];
    plot2.plottingValues = plottingDataValues1;
    plot2.lineColor = [UIColor redColor];
    plot2.lineWidth = 1;
    [_lineChatView  addPlot:plot2];
    
    return _lineChatView;
}


- (UITableView *)walletTableView{
    if (!_walletTableView) {
        _walletTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, Screen_Height-64) style:UITableViewStyleGrouped];
        _walletTableView.delegate = self;
        _walletTableView.dataSource = self;
        _walletTableView.backgroundColor = [UIColor whiteColor];
    }
    return _walletTableView;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    
    return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ID"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ID"];
    }
    
    [cell.contentView addSubview:self.lineChatView];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}


//添加头部视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *headView = [[UIView alloc] init];
    WalletHeaderTableViewCell *headerView = [[[NSBundle mainBundle] loadNibNamed:@"WalletHeaderTableViewCell" owner:nil options:nil] lastObject];
    headerView.frame = CGRectMake(0, 0, Screen_Width, 265);
    [headView addSubview:headerView];
    headerView.Click = ^(){
        [self.navigationController popViewControllerAnimated:YES];
    };

    headerView.totalMoneyLabel.text = [NSString stringWithFormat:@"%.2f", [self.dicM[@"TotalMoney"] floatValue]];
    headerView.wanMoneyLabel.text = [NSString stringWithFormat:@"%.2f", [self.dicM[@"WanMoney"] floatValue]];
    headerView.addMoneyLabel.text = [NSString stringWithFormat:@"%.2f", [self.dicM[@"AddMoney"] floatValue]];
    headerView.dayAnnualLabel.text = [NSString stringWithFormat:@"%.2f", [self.dicM[@"DayAnnual"] floatValue]];
    headerView.yestAmountLabel.text = [NSString stringWithFormat:@"%.2f", [self.dicM[@"YestAmount"] floatValue]];
    
    return headView;
}

//添加footer
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *footerView = [[UIView alloc] init];
    WalletTableViewCell *footCell = [[[NSBundle mainBundle] loadNibNamed:@"WalletTableViewCell" owner:nil options:nil] lastObject];
    footerView.frame = CGRectMake(0, 0, Screen_Width, 170);
    [footerView addSubview:footCell];
    footCell.Click = ^(NSInteger tag){
        switch (tag) {
            case 1001:
            {
                //萬钱宝介绍
                IntroduceViewController *introView = [[IntroduceViewController alloc] init];
                [self setHidesBottomBarWhenPushed:YES];
                [self.navigationController pushViewController:introView animated:YES];

            }
                break;
            case 1002:
            {
                //问题解答
                AnswerViewController *ansView = [[AnswerViewController alloc] init];
                [self setHidesBottomBarWhenPushed:YES];
                [self.navigationController pushViewController:ansView animated:YES];

            }
                break;

            case 1003:
            {
                //资金明细
                ProjectIntroductionViewController *projectView  = [[ProjectIntroductionViewController alloc] init];
                [self setHidesBottomBarWhenPushed:YES];
                [self.navigationController pushViewController:projectView animated:YES];
                
            }
                break;
            case 1004:
            {
                //余额不足一元
               
//                if ([[[NSUserDefaults standardUserDefaults]objectForKey:@"totalAmount"] integerValue] < 1.00) {
//                    
//                    [self signIn:@"转入提醒" and:@"当前账户余额不足1.00元，请充值足额后转入" andType:1004];
//                   
//                }
//                else{
                    [self signIn:@"转入提醒" and:@"￥0.00" andType:1004];
//                }

            }
                break;
            case 1005: {
               
                [self signIn:@"转出提醒" and:@"￥0.00" andType:1005];
            }
            default:
                break;
        }
    };
    return footerView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 170;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    return 270;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 265;
}



//点击转出 调用[[UIApplication sharedApplication] keyWindow];
- (void)signIn:(NSString *)title and:(NSString *)detail andType:(NSInteger)type{
    UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
    _window = window;
    [UIView  animateWithDuration:0.2 animations:^{
        _signView = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, Screen_Height)];
        _signView.backgroundColor = [UIColor colorWithRed:0.16 green:0.17 blue:0.21 alpha:0.5];
        [window addSubview:_signView];
    }];
    
    [_signView addTarget:self action:@selector(touchUp:) forControlEvents:UIControlEventTouchUpInside];
    
    UIView *sgnIn = [[UIView alloc] init];
    sgnIn.backgroundColor = [UIColor whiteColor];
    sgnIn.layer.cornerRadius = 10;
    sgnIn.layer.masksToBounds = YES;
    sgnIn.userInteractionEnabled = YES;
    [_signView addSubview:sgnIn];
    
    [sgnIn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(_signView);
        make.width.mas_equalTo(270);
        make.height.mas_equalTo(175);
       
    }];
    
    
    
    UILabel *stateLable = [[UILabel alloc] init];
    UIButton *sureButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 175-40, 135, 40)];
    UIButton *closeBtn = [[UIButton alloc] initWithFrame:CGRectMake(135,175-40, 135, 40)];
    
    
    sureButton.layer.borderWidth = 1;
    sureButton.layer.borderColor = [UIColor colorWithRed:0.88 green:0.88 blue:0.88 alpha:1.00].CGColor;
    closeBtn.layer.borderWidth = 1;
    closeBtn.layer.borderColor = [UIColor colorWithRed:0.88 green:0.88 blue:0.88 alpha:1.00].CGColor;
    
    stateLable.text = title/*@"转入提醒"*/;
    stateLable.textColor = [UIColor lightGrayColor];
    stateLable.font = [UIFont systemFontOfSize:20.0f];
    
    [sgnIn addSubview:stateLable];
    [sgnIn addSubview:sureButton];
    [sgnIn addSubview:closeBtn];
    
    
    [sureButton setTitle:@"确定" forState:UIControlStateNormal];
    [closeBtn setTitle:@"关闭" forState:UIControlStateNormal];
    [sureButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [closeBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    sureButton.tag = type;
    [sureButton addTarget:self action:@selector(nextBtn:) forControlEvents:UIControlEventTouchUpInside];
    [closeBtn addTarget:self action:@selector(touchUp:) forControlEvents:UIControlEventTouchUpInside];
    
    [stateLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(sgnIn.mas_centerX);
        make.topMargin.mas_equalTo(20);
    }];
    
    //余额小于1元
//    if ([[[NSUserDefaults standardUserDefaults]objectForKey:@"totalAmount"] integerValue] < 1.00) {
    if (type == 0) {
        UILabel *content = [[UILabel alloc] init];
        content.numberOfLines = 2;
        content.text = detail/*@"当前账户余额不足1.00元，请充值足额后转入"*/;
        content.font = [UIFont systemFontOfSize:15.0f];
        content.textAlignment = NSTextAlignmentCenter;
        [sgnIn addSubview:content];
        
        [content mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(sgnIn.mas_centerX);
            make.topMargin.mas_equalTo(stateLable.mas_bottom).offset(40);
            make.leftMargin.mas_equalTo(15);
            make.rightMargin.mas_equalTo(-15);
        }];   
    }else{
        //余额大于1.00
        UITextField *amount = [[UITextField alloc] init];
        _amount = amount;
        amount.textAlignment=NSTextAlignmentCenter;
        amount.font = [UIFont systemFontOfSize:30.00f];
        [sgnIn addSubview:amount];
        amount.placeholder = detail;
        
        [amount mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(sgnIn.mas_centerX);
            make.topMargin.mas_equalTo(stateLable.mas_bottom).offset(40);
            make.leftMargin.mas_equalTo(15);
            make.rightMargin.mas_equalTo(-15);
        }];
        

    }
    

    
}

/**
 *  转出金额接口
 */
- (void)rollOutMoney {
    
    /*
     用户 Id     UserId     int     是
     标 Id     TendId     int     是
     金额     Amount     Decimal(18,2)     是
     Key     Key     是
     */
    
//    NSString *userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
//    NSDictionary *dict = @{
//                           @"TendId":self.dueOnDemandDicM[@"Id"],
//                           @"UserId":userId,
//                           @"Amount":_amount.text
//                           };
//    [NetWorkManager requestWithType:HttpRequestTypePost withUrlString:@"Amount/RollOutTend" withMethodName:@"RollOutTend" withParaments:dict withSuccessBlock:^(NSDictionary *object) {
//        NSLog(@"%@", object);
////        [self signIn:@"转出提醒" and:object[@"message"]];
//        if([object[@"code"] isEqualToString:@"100"]){
//            [ToastView showInfoWithStatus:object[@"message"]];
//            [self getData];
//        }else if([object[@"code"] isEqualToString:@"-100"]){
//            [ToastView showInfoWithStatus:object[@"message"]];
//            //当前账户余额不足,请充值足额后转入
//            
//            
//        }else if([object[@"code"] isEqualToString:@""]){
//            [ToastView showInfoWithStatus:object[@"message"]];
//        }
//        
//    } withFailureBlock:^(NSError *error) {
//        
//    } progress:^(float progress) {
//        
//    }];
}

/**
 *  转入金额接口
 */
- (void)RollInMoney {
    //http://114.215.198.5:22853/ Amount/ RollInTend 转入
    /**
     用户 Id   UserId
     标 Id     TendId
     金额      Amount
     Key       Key
     */
//    NSString *useId = [[NSUserDefaults standardUserDefaults] objectForKey:@"userId"];
//    
//    NSDictionary *para = @{@"userId":useId,@"TendId":self.dueOnDemandDicM[@"Id"],@"Amount":_amount.text};
//    
//    [NetWorkManager requestWithType:HttpRequestTypePost withUrlString:@"Amount/RollInTend" withMethodName:@"RollInTend" withParaments:para withSuccessBlock:^(NSDictionary *object) {
//        
//        NSLog(@"%@",object[@"message"]);
//        
//        if([object[@"code"] isEqualToString:@"100"]){
//            [ToastView showInfoWithStatus:object[@"message"]];
//            [self getData];
//        }else if([object[@"code"] isEqualToString:@"-100"]){
//            
//            if ([[[NSUserDefaults standardUserDefaults]objectForKey:@"totalAmount"] integerValue] < 100000) {
//                [ToastView showInfoWithStatus:object[@"message"]];
//                MyPayViewController *topUp = [[MyPayViewController alloc]init];
//                [self setHidesBottomBarWhenPushed:YES];
//                [self.navigationController pushViewController:topUp animated:YES];
//            }
//            [ToastView showInfoWithStatus:object[@"message"]];
//            //当前账户余额不足,请充值足额后转入
//            
//            
//        }else if([object[@"code"] isEqualToString:@""]){
//            [ToastView showInfoWithStatus:object[@"message"]];
//        }
//        
//    } withFailureBlock:^(NSError *error) {
//        
//    } progress:nil];

}

//释放windows
- (void)touchUp:(UIButton *)tap{
    
//    [self getDailyCheck];
    [_signView removeFromSuperview];
}

//跳转到添加银行卡页面
- (void)nextBtn:(UIButton *)tap{
    [_signView removeFromSuperview];
//    if ([[[NSUserDefaults standardUserDefaults]objectForKey:@"totalAmount"] integerValue] < 1.00) {
    if (tap.tag == 0) {
        TopupViewController *topUp = [[TopupViewController alloc]init];
        [self setHidesBottomBarWhenPushed:YES];
        [self.navigationController pushViewController:topUp animated:YES];
    } else if (tap.tag == 1004) {
        [self RollInMoney];
    } else if (tap.tag == 1005) {
        [self rollOutMoney];
    }
   
    
}




@end
